package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.SupplierMerchantReq;
import com.example.xpvideo.entity.req.TopLiveReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.LocalMerchantsDetail;
import com.example.xpvideo.entity.res.MerchantsRes;
import com.example.xpvideo.entity.res.PHPMerchantsDetailRes;
import com.example.xpvideo.entity.res.PhpMerchantsRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LiveBroadcastMapper;
import com.example.xpvideo.mapper.LsShopAdminMapper;
import com.example.xpvideo.mapper.LsShopMapper;
import com.example.xpvideo.mapper.PhpMerchantsMapper;
import com.example.xpvideo.service.PhpMerchantsService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/8/20 11:28
 * @description:
 */
@Service
public class PhpMerchantsServiceImpl implements PhpMerchantsService {
    @Autowired
    private PhpMerchantsMapper phpMerchantsMapper;
    @Autowired
    private LsShopMapper lsShopMapper;
    @Autowired
    private LsShopAdminMapper lsShopAdminMapper;
    @Autowired
    private LiveBroadcastMapper liveBroadcastMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult1<List<PhpMerchantsRes>>> list(SupplierMerchantReq supplierMerchantReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String merchantsName = supplierMerchantReq.getMerchantsName();
        String account = supplierMerchantReq.getAccount();
        Integer stage = supplierMerchantReq.getStage();
        Integer allCount = phpMerchantsMapper.allMount();
        Integer newCount = phpMerchantsMapper.newMount();

        PageHelper.startPage(page, size);
        List<PhpMerchantsRes> list =phpMerchantsMapper.merchantsList (Wrappers.<LsShopAdmin>query()
                .like(!ObjectUtils.isEmpty(merchantsName),"ls.name",merchantsName)
                .eq(!ObjectUtils.isEmpty(account),"account",account)
                .eq(!ObjectUtils.isEmpty(stage),"lsa.disable",stage)
                .eq("type",1));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult1<List<PhpMerchantsRes>> listPageResult = new PageResult1<>(count,allCount,newCount, pages,
                pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<PHPMerchantsDetailRes> detail(Integer id) throws ErrorException {

        PHPMerchantsDetailRes phpMerchantsDetailRes = phpMerchantsMapper.merchantsDetail(Wrappers.<LsShopAdmin>query()
                .eq("lsa.id",id));

        return ResponseResult.responseResult(0, phpMerchantsDetailRes);

    }

    @Override
    public ResponseResult<Object> updateMStage(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsShopAdmin lsShopAdmin = new LsShopAdmin();
        lsShopAdmin.setId(id);
        lsShopAdmin.setDisable(stage);
        lsShopAdmin.setUpdateTime(String.valueOf(timeService.getTime()));
        int res = lsShopAdminMapper.update(lsShopAdmin,Wrappers.<LsShopAdmin>update().eq("id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"冻结/解冻商户成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> topLive(TopLiveReq topLiveReq) throws ErrorException {
        Integer id = topLiveReq.getId();
        Integer stage = topLiveReq.getStage();
        Integer topDays = topLiveReq.getTopDays();

        LiveBroadcast liveBroadcast = new LiveBroadcast();
        liveBroadcast.setIsTop(stage);
        liveBroadcast.setUpdateTime(timeService.getTime());
        int res = liveBroadcastMapper.update(liveBroadcast, Wrappers.<LiveBroadcast>update()
                .set(stage == 1, "top_days", topDays)
                .eq("merchants_id", id));

        if (res > 0) {
            return ResponseResult.responseResult(0, "置顶/取消置顶商户直播间成功！");
        }

        throw new ErrorException(ErrCodeEnum.unknown_20000);

    }


    @Override
    public ResponseResult<Object> recommendLive(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LiveBroadcast liveBroadcast = new LiveBroadcast();
        liveBroadcast.setIsRecommend(stage);
        liveBroadcast.setUpdateTime(timeService.getTime());
        int res = liveBroadcastMapper.update(liveBroadcast,Wrappers.<LiveBroadcast>update().eq("merchants_id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"推荐/取消推荐商户直播间成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> isOffcial(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsShop lsShop = new LsShop();
        lsShop.setId(id);
        lsShop.setIsOffical(stage);
        lsShop.setUpdateTime(timeService.getTime());
        int res = lsShopMapper.update(lsShop,Wrappers.<LsShop>update().eq("id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"设为官方号/取消官方号成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
